package lib.bone.access.config.cycle;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import lib.bone.access.event.CycleCreatedEvent;
import lib.bone.access.event.CycleDeletedEvent;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@Primary
public class NepuCycleServiceTestImpl implements NepuCycleService{
    @Autowired
    NepuCycleDAO dao;
//
//    @Autowired(required = false)
//    CycleCreatedEvent createdEvent;


    // @TODO 待替换
    public static final String currentId = "2c9046928547939801882221e51e0007";

    @Override
    public NepuCycle getNowResearchCycle() {
        Optional<NepuCycle> cycleOp = dao.findById("2c9046928547939801882221e51e0007");
        if (!cycleOp.isPresent()){
            throw new BusinessException(ResponseCodeConst.NOT_FOUND,"当前不在周期内");
        }else {
            return cycleOp.get();
        }
    }

    @Override
    public NepuCycle getPreviousResearchCycle() {
        Optional<NepuCycle> cycleOp = dao.findById("40288186801b7d8101801b7fabb50000");
        if (!cycleOp.isPresent()){
            throw new BusinessException(ResponseCodeConst.NOT_FOUND,"当前不在周期内");
        }else {
            return cycleOp.get();
        }
    }

    @Override
    public String getNowResearchCycleId() {
        return getNowResearchCycle().getId();
    }

    @Override
    public List<NepuCycle> getNowSubCycle() {
        NepuCycle parentCycle = getNowResearchCycle();
        String parentId = parentCycle.getId();
        return dao.findByParentId(parentId);
    }

    //    @Override
//    public Optional<NepuCycle> findNowCycle(Date now) {
//        return dao.findByStartDateLessThanAndEndDateGreaterThan(now,now);
//    }

    @Deprecated
    @Override
    public Optional<NepuCycle> findNowCycle(String cycleType) {
        return dao.findByCycleType(cycleType);
    }

    @Deprecated
    @Override
    public NepuCycle getNowCycle() {
//        Date now = DateUtil.date();
//        Optional<NepuCycle> cycleOp = findNowCycle(now);

        Optional<NepuCycle> cycleOp = dao.findById("2c9046928547939801882221e51e0007");
        if (!cycleOp.isPresent()){
            throw new BusinessException(ResponseCodeConst.NOT_FOUND,"当前不在周期内");
        }else {
            return cycleOp.get();
        }
    }

    @Override
    public String getCycleNameById(String id) {
        NepuCycle nepuCycle = new NepuCycle();
        Optional<NepuCycle>  cycleOptional = dao.findById(id);
        if (cycleOptional.isPresent()){
            BeanUtil.copyProperties(cycleOptional.get(),nepuCycle, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
            return nepuCycle.getCycleName();
        }
        return "周期不存在";
    }

    @Override
    public String getNowCycleId() {
        NepuCycle cycle = getNowCycle();
        return cycle.getId();
    }

    @Override
    public PageResultDTO<NepuCycle> getAll(Pageable pageable, Example<NepuCycle> example) {
        Page<NepuCycle> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }else {
            page = dao.findAll(example,pageable);
        }
        List<NepuCycle> list = page.getContent();
        PageResultDTO<NepuCycle> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO<NepuCycle> deleteById(String id) {
        dao.deleteById(id);
        return ResponseDTO.succMsg("删除成功");    }

    @Override
    public Optional<NepuCycle> getById(String id) {
        return dao.findById(id);
    }

    @Override
    public NepuCycle saveOne(NepuCycle nepuCycle) {
        if (Validator.isNotEmpty(nepuCycle.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        dao.save(nepuCycle);
        //触发事件
        //createdEvent.process(nepuCycle);
        return nepuCycle;
    }

    @Override
    public NepuCycle updateOne(NepuCycle nepuCycle) {
        if (Validator.isEmpty(nepuCycle.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(nepuCycle);
    }
}
